clear all;
clc;

load mnist_train;
load mnist_test;
l = size(xL,2);
n = size(xU,2)+l;
X = [xL,xU;ones(1,n)];
clear xU;
clear xL;
Y = yL';

T = eye(n) - X'*inv(X*X')*X;

nump = c*(c-1)/2;
sz = zeros(c,1);
idx = cell(10,1);
for i = 1:c
    idx{i}=find(Y==(i-1));
    sz(i)=size(idx{i},1);
end

ix = cell(c,c);
Yl = cell(c,c);
Yu = cell(c,c);

for i = 1:c
    for j = i+1:c
        ix{i,j} = [idx{i};idx{j}];
        Yl{i,j} = -inf*ones(n,1);
        Yl{i,j}(idx{i}) = ones(sz(i),1);
        Yu{i,j} = inf*ones(n,1);
        Yu{i,j}(idx{j}) = -ones(sz(j),1);
    end
end

disp('Start optimization...');
tic

fai = zeros(n,nump);

ct = 1;
for i = 1:c
    for j = i+1:c
        ct
        tic
        x = quadprog(T,[],[],[],[],[],Yl{i,j},Yu{i,j});
        fai(:,ct) = x;
        ct = ct+1;
        toc
    end
end